package com.coocaa.ops.admin.v1.cornericon.service.impl;

import com.coocaa.ops.admin.v1.cornericon.biz.impl.CornerIconBizImpl;
import com.coocaa.ops.admin.v1.cornericon.converter.CornerIconQueryBeanConverter;
import com.coocaa.ops.admin.v1.cornericon.model.GlobalCornerIconModel;
import com.coocaa.ops.admin.v1.cornericon.model.IconAttributeModel;
import com.coocaa.ops.admin.v1.cornericon.provider.impl.CornerIconQueryProviderImpl;
import com.coocaa.ops.admin.v1.cornericon.service.ICornerIconQueryService;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/6/19.
 * @description: 角标 查询服务实现
 */
@Service
public class CornerIconQueryServiceImpl implements ICornerIconQueryService {

    @Autowired
    private CornerIconQueryProviderImpl cornerIconQueryProvider;

    @Autowired
    private CornerIconBizImpl cornerIconBiz;

    @Override
    public GlobalCornerIconEntity getDetailInfo(Integer id) {
        GlobalCornerIconEntity iconEntity = cornerIconQueryProvider.getDetailInfo(id);
        //TODO 1期只支持VIP，后面会改用调媒资库接口获取角标类别名称
        //"0"-其他，“1”-VIP
        if ("0".equals(iconEntity.getAttributeCode())) {
            iconEntity.setAttributeName("other");
        } else if ("1".equals(iconEntity.getAttributeCode())) {
            iconEntity.setAttributeName("VIP");
        }
        return iconEntity;
    }

    @Override
    public Map<String, Object> getPageResult(GlobalCornerIconModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<GlobalCornerIconEntity> pageResult = cornerIconBiz.getCommonPageList(params, false);
        return R.ok(pageResult);
    }


    @Override
    public Map<String, Object> getIconAttributes() {
        List<IconAttributeModel> iconAttributes = listIconAttribute();
        Map<String, Object> result = InstanceUtil.newHashMap(3);
        result.put("attributes", iconAttributes);
        result.put("success", true);
        result.put("code", "0");
        return result;
    }

    /**
     * 获取角标类别
     *
     * @return
     */
    private List<IconAttributeModel> listIconAttribute() {
        List<IconAttributeModel> result = new ArrayList<>();
        List<IconAttributeModel> attributes = CornerIconQueryBeanConverter.getAllAttributes();
        if (CollectionUtils.isNotEmpty(attributes)) {
            //获取已使用的角标类别code
            List<String> usedAttributeCodes = cornerIconBiz.getMapper().listUsedAttributeCode();
            if (CollectionUtils.isNotEmpty(usedAttributeCodes)) {
                for (String usedAttribute : usedAttributeCodes) {
                    for (IconAttributeModel attribute : attributes) {
                        if (attribute.getAttributeCode().equals(usedAttribute)) {
                            attribute.setDisabled(true);
                        }
                    }
                }
            }
            result.addAll(attributes);
        }
        result.add(new IconAttributeModel("0", "other", false));
        return result;
    }


}
